package com.calvin.lingting.actions;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.calvin.lib.EventDispatch;
import com.calvin.lib.annotation.ViewInject;
import com.calvin.lib.json.JSONArray;
import com.calvin.lib.notification.NotificationCenter;
import com.calvin.lib.utils.CUtils;
import com.calvin.lingting.MainActivity;
import com.calvin.lingting.R;
import com.calvin.lingting.adapter.BookListViewAdapter;
import com.calvin.lingting.adapter.DwonloadBookListViewAdapter;
import com.calvin.lingting.config.NTConfig;
import com.calvin.lingting.service.SerializationCenter;

public class BookListAction {
    public static final int T_LOCAL = 0;
    public static final int T_LIKE = 1;
    public static final int T_RECORD = 2;
    public static final int T_DWONNLOAD = 3;

    int mType = 0;

    @ViewInject(id = R.id.listView)
    ListView mListView;

    JSONArray mBookList = new JSONArray();


    public BookListAction(int type) {
        mType = type;
    }

    @ViewInject(isTag = true, tag = "nav_title", listen = EventDispatch.CListen.INIT)
    public void onInit(View view) {

        int titleId = 0;
        switch (mType) {
            case T_LOCAL:
                titleId = R.string.s_local_book;
                break;
            case T_LIKE:
                titleId = R.string.s_like_book;
                break;
            case T_RECORD:
                titleId = R.string.s_record_book;
                break;
            case T_DWONNLOAD:
                titleId = R.string.s_download_book;
                mListView.addHeaderView(View.inflate(MainActivity.sharedCurrent(),R.layout.list_header,null));
                break;
        }
        CUtils.cast(view, TextView.class).setText(titleId);

        buildBookList();
        mListView.setAdapter(mType == T_DWONNLOAD ? new DwonloadBookListViewAdapter(MainActivity.sharedCurrent(), mBookList) : new BookListViewAdapter(MainActivity.sharedCurrent(), mBookList));
    }

    public void buildBookList() {
        switch (mType) {
            case T_LOCAL:
                mBookList = SerializationCenter.shared().getDwonloadBookList();
                break;
            case T_RECORD:
                mBookList = SerializationCenter.shared().getRecordBookList();
                break;
            case T_LIKE:
                mBookList = SerializationCenter.shared().getLikeBookList();
                break;
            case T_DWONNLOAD:
                mBookList = new JSONArray();
                mBookList.add(10);
                mBookList.add(10);
                break;
        }
    }

    @ViewInject(isTag = true, tag = "nav_left_button")
    public void onNavLeftClick(View view) {
        NotificationCenter.shared().postNotice(NTConfig.NT_NAV_BACK);
    }

    @ViewInject(id = R.id.listView, listen = EventDispatch.CListen.ITEMCLICK)
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (mType){
            case T_DWONNLOAD:
                break;
            case T_RECORD:
            default:
                NotificationCenter.shared().postNotice(NTConfig.NT_NAV_NEXT, R.layout.action_detail, new DetailAction(mBookList.getJSONObject(position)));
                break;
        }

    }

}
